package com.globallogic.week3.acceptance;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;

import com.globallogic.week3.config.JettyServer;

public abstract class BaseAcceptanceConfig {
	private static final String REGISTRATION_URL = "/register";
	protected static WebDriver browser = new HtmlUnitDriver();
	protected static JettyServer server = new JettyServer();	
	
	protected static final String LOGIN = "user";
	protected static final String PASSWORD = "user123";
	
	protected abstract String getPageUrl();
	
	@BeforeClass
	public static void setUpClass(){
		server.start();
		registerUser(LOGIN, PASSWORD, PASSWORD);
		logout();
	}
	
	@AfterClass
	public static void tearDownClass(){
		browser.quit();
		server.stop();
	}
	
	@Before
	public void setUp(){
		browser.get(server.getURL(getPageUrl()));
	}
	
	public static void registerUser(String username, String password, String passwordConfirm){		
		browser.get(server.getURL(REGISTRATION_URL));
		WebElement loginInput = browser.findElement(By.name("login"));
		WebElement passInput = browser.findElement(By.name("pass"));
		WebElement conPassInput = browser.findElement(By.name("conPass"));
		WebElement registerButton = browser.findElement(By.name("register"));
		  
		loginInput.sendKeys(username);
		passInput.sendKeys(password);
		conPassInput.sendKeys(passwordConfirm); 
        registerButton.click();
	}
	
	public static void logout(){
		browser.get(server.getURL(""));
		browser.findElement(By.linkText("Logout")).click();
	}
}
